/**
 * Created by DELL on 2018/9/20.
 */
(function () {
  window.onload = function () {
    share.init();
  };
  var share = window.share = {
    workid: '',   // 作品id
    is_playingMusic: false, // 是否正在播放音乐
    bgAudio: null,         // 背景音乐audio
    playData: [],           // 播放数据
    playing_audio_index: 0,      // 播放中 第几个音频文件
    workAudios: [],
    current_playing_time: 0,     // 当前播放时间
    playWorkManage: null,        // 作品播放管理器
    init: function () {
      this.playWorkManage = new PlayWork();
      this.getWork();
      this.initEvent();
    },
    // 获取作品详情
    getWork: function () {
      var _this = this;
      var workid = tools.getParams().workid;
      if (!workid) {
        console.log('分享链接参数错误');
        if (window.location.hostname === 'www.newscctv.net') {// 央视网直接回到首页
          alert('分享链接参数错误, 3s后跳转活动首页');
          setTimeout(function () {
            window.location.href = './index.html';
          }, 3000);
        }

        return false
      }
      this.workid = workid;
      $.ajax({
        url: _api.getUserPlayInfo,
        type: 'post',
        dataType: 'json',
        data: {
          uuid: workid
        }
      }).done(function (resp) {
        if (resp.code === 0) {
          var data = resp.data;
          var nickname = data.user_info.nickname;
          var music_name = data.music_info.title

          var playData = JSON.parse(data.play_data);
          _this.playData = playData;
          console.log(playData, 'playData');

          var score = playData.length;
          _this.configWXsdk({
            link: _config.WXsharebaseLink + '?workid=' + workid,
            title: nickname + '弹奏了一曲《' + music_name + '》,获得' + score + '分,邀你来弹~',
            desc: '',
            imgUrl: ''
          });

          if (nickname.length > 5) {
            nickname = nickname.substr(0, 5) + '...'
          }

          $('.music_dis p').html('<span>' + nickname + ' </span>在央视新闻弹奏了《' + data.music_info.title + '》</span>，获得了 <span><b>' + score + '</b></span> 分，邀你来弹~');
          $('.cd_cover').css({
            'background-image': 'url(' + data.music_info.cover + ')'
          });
          var format_num = tools.numToString(data.zan_num);
          $('#zan_num').attr('data-num', data.zan_num).text(format_num);

          // 初始化按键

          // _this.initPlayWorkAudios(data.music_info.id, playData);
          _this.playWorkManage.init(data.music_info.id, playData, _this.playEnd.bind(_this));
          $('.loading').hide();
        } else {
          alert('获取用户作品失败: ' + resp.msg);
        }
      }).fail(function (err) {
        alert('获取用户作品失败: 内部服务器错误');
      });
    },
    initEvent: function () {
      var _this = this;
      $('.btn_go').click(function () {
        window.location.href = './index.html';
      });
      // 点赞
      $('.btn_zan').click(function () {
        _this.zanAction();
      });
      // 播放
      $('.btn_play_music').click(function () {
        _this.playMusic_click();
      });
      $('.cd_handle').click(function () {
        _this.playMusic_click();
      })
    },
    // 点赞
    zanAction: function () {
      var zaNum_dom = $('#zan_num');
      var origin_num = zaNum_dom.attr('data-num') / 1;
      var new_num = origin_num + 1;
      var format_num = tools.numToString(new_num);
      zaNum_dom.text(format_num);
      zaNum_dom.attr('data-num', new_num);
      $.ajax({
        url: _api.zanPlay,
        type: 'POST',
        dataType: 'json',
        data: {
          uuid: this.workid
        }
      }).done(function (resp) {
        if (resp.code === 0) {
        } else {
        }
      });
      this.zanAnimate();
    },
    // 点赞动画
    zanAnimate: function () {

      var _id = 'img_' + new Date().getTime();
      $(".zan_animate_box").append('<img id="' + _id + '" class="zanfly" src="images/zan1.png">');
      var _left_arr = ['-0.3rem', '-0.2rem', '0rem', '0.3rem', '0.5rem'];

      var _left = _left_arr[Math.round(Math.random() * _left_arr.length)];

      $('#' + _id).velocity(
        {
          bottom: "5.5rem",
          opacity: "0",
          // rotateZ: 360 * 2,
          scale: 1.5,
          left: _left
        }, {
          complete: function (elements) {
            $(elements).remove();
          },
          duration: 2000
        }
      );
    },
    configWXsdk: function (obj) {
      var wxconfig = new WxConfig();
      var appid = _config.appID;
      var link = obj.link || _config.appBaseLink;
      var title = obj.title || '我为祖国献歌一曲';
      var desc = obj.desc || '弹得真好，忍不住单曲循环，余音不绝啊';
      var imgUrl = obj.imgUrl || _config.WXsharebaseIcon;
      wxconfig.init({
        appid: appid,
        url: window.location.href,
        link: link,
        debug: false,
        title: title,
        desc: desc,
        imgUrl: imgUrl
      });
    },
    //
    playMusic_click: function () {
      var _this = this;
      if (!this.is_playingMusic) {
        this.is_playingMusic = true;
        $('.btn_play_music').fadeOut();
        $('.cd_handle').addClass('playing_handel');
        _this.playWorkManage.start_play();
        setTimeout(function () {
          $('.disc_box').addClass('playing_disc');
        }, 300);
      }
    },
    // 音频播放结束
    playEnd: function () {
      $('.cd_handle').removeClass('playing_handel');
      $('.disc_box').removeClass('playing_disc');
      $('.btn_play_music').fadeIn();
      this.is_playingMusic = false;
    }

  }

})(window);